package zad2;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.TimeUnit;


public class Zad2test {
	public static void main(String[] args) {
		
		final Source source = new Source(40);
		
		final Random r= new Random();
		
		ArrayList<Thread> threds = new ArrayList<Thread>();
		
		
		//Tworzenie konsumentow
		
		for (int i = 0; i < 100; i++) {
			threds.add(new Thread(){
				@Override
				public void run() {
					while(true){
						System.out.println("Rozpocznanie konsumpcji: " + getName());
						
						//Konsumpcja
						String value = source.get();
						
						
						System.out.println( getName() + " pobierz wartosc: " +value);
						
						try {
							TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			});
		};
		
		//Tworzenie Producentow
		for (int i = 0; i < 1000; i++) {
			threds.add(new Thread(){
				@Override
				public void run() {
					while(true){
						System.out.println("Rozpoczynanie produkcji:  " + getName());
						String value = getName() + " @ " + System.nanoTime(); 
						
						//produkcja
						source.putNew(value);
						
						System.out.println(getName() + " skonczyl produkcje: " + value);
						try {
							TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			});
		};
		
		for (Thread thread : threds) {
			thread.start();
		}
	}
}
